//
//  CPAuthorByAlphaVC.m
//  ChinaPoem
//
//  Created by 赵俊明 on 2019/3/10.
//  Copyright © 2019 赵俊明. All rights reserved.
//

#import "CPAuthorByAlphaVC.h"
#import "CPAuthContentController.h"
@interface CPAuthorByAlphaVC ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, weak) UITableView *tabView;
@property (nonatomic, strong) NSMutableArray *dataSource;
@end

@implementation CPAuthorByAlphaVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = _alpha;
    self.view.backgroundColor = [UIColor whiteColor];
    UITableView *sdBarTabView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kW, kH) style:(UITableViewStylePlain)];
    [sdBarTabView registerClass:[CPAuthorCell class] forCellReuseIdentifier:@"authorCell"];
    sdBarTabView.delegate = self;
    sdBarTabView.dataSource = self;
    sdBarTabView.backgroundColor = [UIColor clearColor];
    sdBarTabView.separatorColor = JMColorRGBA(230, 230, 230, 0.8);
    sdBarTabView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:sdBarTabView];
    self.tabView = sdBarTabView;
    [sdBarTabView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(self.view);
        if (@available(iOS 11.0, *)) {
            make.top.mas_equalTo(self.view.mas_safeAreaLayoutGuideTop);
            make.bottom.mas_equalTo(self.view.mas_safeAreaLayoutGuideBottom);
        } else {
            make.top.mas_equalTo(self.view.mas_top);
            make.bottom.mas_equalTo(self.view.mas_bottom);
        }
    }];
    [self pullRefresh];
}

- (void)pullRefresh
{
    JMSelf(weakSelf);
    self.tabView.mj_header= [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        weakSelf.dataSource = [[CPSQLTools cp_sharedInstance] cp_authorByAlpha:weakSelf.alpha];
        sleep(.5);
        dispatch_async(dispatch_get_main_queue(), ^{
            [weakSelf.tabView reloadData];
            [weakSelf.tabView.mj_header endRefreshing];
        });
    }];
    
    [self.tabView.mj_header beginRefreshing];
}

#pragma mark --
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ID = @"authorCell";
    CPAuthorCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {cell = [[CPAuthorCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:ID];}
    cell.model = _dataSource[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    CPAuthContentController *authorContent = [[CPAuthContentController alloc] init];
    authorContent.model = _dataSource[indexPath.row];
    [self.navigationController pushViewController:authorContent animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100.0;
}

@end
